package willand.com.cloud.clickhouse.clickhousejdbc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import willand.com.cloud.clickhouse.clickhousejdbc.model.OrderDO;
import willand.com.cloud.clickhouse.clickhousejdbc.service.ClickhouseService;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClickhouseServiceImpl implements ClickhouseService {

    //
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(List<OrderDO> orders) {

        String sql = "insert into willand_cloud_table_002 " +
                " values " +
                "(101001,'sku_001',1000.00,'2020-06-01 12:00:00');";

        // 准备数据
        List<Object[]> batch = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            jdbcTemplate.update(sql);
        }

    }

    @Override
    public List<OrderDO> getOrders(int count) {

        HttpClient client = HttpClient.newHttpClient();

        // 创建HttpRequest实例，指定GET方法和URI
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://10.139.0.39:9123/?query=SELECT%20count(*)%20FROM%20willand_cloud_002.t_vehicle_status_distributed%20WHERE%20eventTime%20%3E%3D%20%272025-01-01%2000%3A00%3A00%27%20and%20eventTime%20%3C%20%272025-01-01%2019%3A03%3A10%27%20%20limit%2010")) // 替换为你需要请求的URL
                .header("X-ClickHouse-User", "")
                .header("X-ClickHouse-Key", "")
                .header("User-Agent", "shardAll")
                .build();

        // 发送GET请求
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 打印响应状态码和体
            System.out.println("Status Code: " + response.statusCode());
            System.out.println("Response Body: " + response.body());
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


}
